package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/models"
	"strconv"
	"time"
)

func PersonAdd(c *gin.Context) {
	per_code := c.PostForm("per_code")
	per_name := c.PostForm("per_name")
	per_phone := c.PostForm("per_phone")
	data := models.Person{
		PerCode:  per_code,
		PerName:  per_name,
		PerPhone: per_phone,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PerOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_to := c.PostForm("order_to")
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	data := models.PersonOrder{
		OrderCode: order_code,
		OrderTo:   order_to,
		Pid:       pid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderList(c *gin.Context) {
	/*
			SELECT
		  person_orders.id AS OrderId,
		  person_orders.order_code AS OrderCode,
		  person_orders.order_to AS OrderTo,
		  people.per_code AS PerCode,
		  people.per_name AS PerName,
		  people.per_phone AS PerPhone
		FROM
		  person_orders
		  LEFT JOIN people ON people.id = person_orders.pid
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	var OrderList []models.PerSonOrderList
	redis_key := "person_order_list_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("person_orders").
			Select(
				"person_orders.id AS OrderId",
				"person_orders.order_code AS OrderCode",
				"person_orders.order_to AS OrderTo",
				"people.per_code AS PerCode",
				"people.per_name AS PerName",
				"people.per_phone AS PerPhone",
			).
			Joins("LEFT JOIN people ON people.id = person_orders.pid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&OrderList)
		jsonData, _ := json.Marshal(&OrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &OrderList)
	}
	var total int64
	models.DB.Model(&models.PersonOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  OrderList,
		"total": total,
	})
	return
}
func OrderDetail(c *gin.Context) {
	/*
			SELECT
		  person_orders.id AS OrderId,
		  person_orders.order_code AS OrderCode,
		  person_orders.order_to AS OrderTo,
		  people.per_code AS PerCode,
		  people.per_name AS PerName,
		  people.per_phone AS PerPhone
		FROM
		  person_orders
		  LEFT JOIN people ON people.id = person_orders.pid
	*/
	id := c.Query("id")
	var OrderList models.PerSonOrderList
	redis_key := "person_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("person_orders").
			Select(
				"person_orders.id AS OrderId",
				"person_orders.order_code AS OrderCode",
				"person_orders.order_to AS OrderTo",
				"people.per_code AS PerCode",
				"people.per_name AS PerName",
				"people.per_phone AS PerPhone",
			).
			Joins("LEFT JOIN people ON people.id = person_orders.pid").
			Where(" person_orders.id =?", id).
			Debug().
			Find(&OrderList)
		jsonData, _ := json.Marshal(&OrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &OrderList)
	}
	var total int64
	models.DB.Model(&models.PersonOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  OrderList,
		"total": total,
	})
	return
}
